/**
 * @package com.shm.core
 * @author White
 * @date 2019/11/24
 * @version V1.0
 */
package com.white.shm.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 保存Spring 上下文
 * @author White
 */
public class SpringContextUtil implements ApplicationContextAware {
    
    /** Spring 上下文 */
    private static ApplicationContext context;

    /**
     * Gets context.
     * @return the context
     */
    public static ApplicationContext getContext() {
        return context;
    }

    /**
     * Sets context.
     * @param context the context
     */
    public static void setContext(ApplicationContext context) {
        SpringContextUtil.context = context;
    }

    /**
     * Gets bean.
     * @param beanName the bean name
     * @return the bean
     */
    public static Object getBean(String beanName) {
        return getContext().getBean(beanName);
    }

    /**
     * Gets bean.
     * @param beanName     the bean name
     * @param requiredType the required type
     * @return the bean
     */
    public static <T> T getBean(String beanName, Class<T> requiredType) {
        return getContext().getBean(beanName, requiredType);
    }

    /**
     * Gets bean.
     * @param <T>          the type parameter
     * @param requiredType the required type
     * @return the bean
     */
    public static <T> T getBean(Class<T> requiredType) {
        return getContext().getBean(requiredType);
    }

    /**
     * Sets application context.
     * @param context the context
     */
    @Override
    public void setApplicationContext(ApplicationContext context) {
        setContext(context);
    }

}
